package com.wmlive.hhvideo.heihei.mainhome.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wmlive.hhvideo.common.base.DcBaseFragment;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.main.MultiTypeVideoBean;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoItem;
import com.wmlive.hhvideo.heihei.mainhome.activity.VideoListActivity;
import com.wmlive.hhvideo.heihei.mainhome.adapter.ExplosionAdapter;
import com.wmlive.hhvideo.heihei.mainhome.presenter.ExplosionPresenter;
import com.wmlive.hhvideo.heihei.mainhome.view.ExplosionView;
import com.wmlive.hhvideo.utils.DensityUtils;
import com.wmlive.hhvideo.widget.refreshrecycler.OnFooterClickListener;
import com.wmlive.hhvideo.widget.refreshrecycler.OnRecyclerItemClickListener;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * A simple {@link Fragment} subclass.
 * 新爆
 * Modify by lsq
 */
public class ExplosionFragment extends DcBaseFragment implements
        ExplosionView, SwipeRefreshLayout.OnRefreshListener,
        RefreshRecyclerView.OnLoadMoreListener {


    @BindView(R.id.recycle_explosion)
    RefreshRecyclerView recycleExplosion;
    private ExplosionPresenter explosionPresenter;
    private ExplosionAdapter adapter;
    private int space;


    public static ExplosionFragment newInstance() {
        ExplosionFragment fragment = new ExplosionFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(LAZY_MODE, true);
        bundle.putBoolean(SINGLE_MODE, true);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_explosion;
    }

    @Override
    protected void initData() {
        super.initData();
        space = DensityUtils.dip2px(getActivity(), 1);
        explosionPresenter = new ExplosionPresenter(this);
        recycleExplosion.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
        recycleExplosion.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) % 2 == 0) {//左
                    outRect.top = space;
                    outRect.bottom = space;
                    outRect.left = 0;
                    outRect.right = space;

                } else {//右
                    outRect.top = space;
                    outRect.bottom = space;
                    outRect.right = 0;
                    outRect.left = space;

                }
            }
        });
        recycleExplosion.setOnRefreshListener(this);
        recycleExplosion.setOnLoadMoreListener(this);

        adapter = new ExplosionAdapter(new ArrayList<ShortVideoItem>(), recycleExplosion);
        recycleExplosion.setFooter(R.layout.view_recycler_main_load_more);
        recycleExplosion.setOffset();
        recycleExplosion.setAdapter(adapter);
        adapter.setOnRecyclerItemClickListener(new OnRecyclerItemClickListener<ShortVideoItem>() {
            @Override
            public void onRecyclerItemClick(int dataPosition, View view, ShortVideoItem data) {
                VideoListActivity.startActivity(getActivity(), RecommendFragmentNew.TYPE_EXPLOSION,
                        MultiTypeVideoBean.createExplosionParma(dataPosition, adapter.getDataContainer()));
            }
        });
        recycleExplosion.setOnFooterClickListener(new OnFooterClickListener() {
            @Override
            public void onPageErrorClick() {
                super.onPageErrorClick();
                onRefresh();
            }

            @Override
            public void onFootErrorClick() {
                super.onFootErrorClick();
                onLoadMore();
            }

        });
        recycleExplosion.autoRefresh(300);
    }

    @Override
    protected void onSingleClick(View v) {

    }


    @Override
    public void handleExplosionSucceed(boolean isRefresh, List<ShortVideoItem> shortVideoInfoList, boolean hasMore) {
        adapter.addData(isRefresh, shortVideoInfoList, hasMore);
    }

    @Override
    public void onRequestDataError(int requestCode, String message) {
        if (requestCode == HttpConstant.TYPE_EXPLOSION_VIDEO
                || requestCode == (HttpConstant.TYPE_EXPLOSION_VIDEO + 1)) {
            adapter.showError(requestCode == HttpConstant.TYPE_EXPLOSION_VIDEO);
        } else {
            super.onRequestDataError(requestCode, message);
        }
    }

    @Override
    public void onRefresh() {
        explosionPresenter.explosionVideo(true);
    }

    @Override
    public void onLoadMore() {
        explosionPresenter.explosionVideo(false);
    }
}
